<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li>
            <router-link :to="{ name: 'login' }">登入</router-link>
          </li>
          <li class="layui-this">
            <!-- 找回密码 -->
            重置密码
          </li>
        </ul>
        <div
          class="layui-form layui-tab-content"
          id="LAY_ucm"
          style="padding: 20px 0;"
        >
          <div class="layui-tab-item layui-show">
            <!-- 重置密码 -->
            <!--
          <div class="fly-msg">{{d.username}}，请重置您的密码</div>
          <div class="layui-form layui-form-pane"  style="margin-top: 30px;">
            <form action="/user/repass" method="post">
              <div class="layui-form-item">
                <label for="L_pass" class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                  <input type="password" id="L_pass" name="pass" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">6到16个字符</div>
              </div>
              <div class="layui-form-item">
                <label for="L_repass" class="layui-form-label">确认密码</label>
                <div class="layui-input-inline">
                  <input type="password" id="L_repass" name="repass" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <label for="L_vercode" class="layui-form-label">人类验证</label>
                <div class="layui-input-inline">
                  <input type="text" id="L_vercode" name="vercode" required lay-verify="required" placeholder="请回答后面的问题" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid">
                  <span style="color: #c00;">{{d.vercode}}</span>
                </div>
              </div>
              <div class="layui-form-item">
                <input type="hidden" name="username" value="{{d.username}}">
                <input type="hidden" name="email" value="{{d.email}}">
                <button class="layui-btn" alert="1" lay-filter="*" lay-submit>提交</button>
              </div>
            </form>
          </div>

          <div class="fly-error">该重置密码链接已失效，请重新校验您的信息</div>
          <div class="fly-error">非法链接，请重新校验您的信息</div>
            -->

            <div class="layui-form layui-form-pane">
              <ValidationObserver ref="observer" v-slot="{validate}">
              <form method="post">
                <div class="layui-form-item">
                  <label class="layui-form-label">新密码</label>
                  <ValidationProvider
                    rules="required|min:6"
                    v-slot="{ errors }"
                    vid="password"
                  >
                    <div class="layui-input-inline">
                      <input
                        type="password"
                        name="password"
                        v-model.trim="password"
                        placeholder="请输入新密码"
                        autocomplete="off"
                        class="layui-input"
                      />
                    </div>
                    <div class="layui-form-mid form-error">{{ errors[0] }}</div>
                  </ValidationProvider>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">确认密码</label>
                  <ValidationProvider
                    rules="confirmed:password"
                    v-slot="{ errors }"
                  >
                    <div class="layui-input-inline">
                      <input
                        type="password"
                        name="checkpass"
                        v-model="checkpass"
                        placeholder="请再次输入密码"
                        autocomplete="off"
                        class="layui-input"
                      />
                    </div>
                    <div class="form-error">{{ errors[0] }}</div>
                  </ValidationProvider>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">验证码</label>
                  <ValidationProvider
                    rules="required|length:4"
                    v-slot="{ errors }"
                    name = "code"
                  >
                    <div class="layui-input-inline">
                      <input
                        type="text"
                        name="code"
                        v-model="code"
                        placeholder="请输入验证码"
                        autocomplete="off"
                        class="layui-input"
                      />
                    </div>
                    <span class="svg" @click="getCaptcha()" v-html="svg"></span>
                    <div class="form-error">{{ errors[0] }}</div>
                  </ValidationProvider>
                </div>
                <div class="layui-form-item">
                  <button type="button" class="layui-btn" @click="validate().then(submit)">
                    提交
                  </button>
                </div>
              </form>
              </ValidationObserver>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ValidationProvider, ValidationObserver } from 'vee-validate'
import { getCode, resetUserPass } from '@/api/login.js'
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'reset',
  data () {
    return {
      password: '',
      code: '',
      checkpass: '',
      svg: '',
      key: ''
    }
  },
  components: {
    ValidationProvider,
    ValidationObserver
  },
  mounted () {
    this.getKey()
    let sid = ''
    if (localStorage.getItem('sid')) {
      sid = localStorage.getItem('sid')
    } else {
      sid = uuidv4()
      localStorage.setItem('sid', sid)
    }
    this.$store.commit('setSid', sid)
    this.getCaptcha()
  },
  methods: {
    getKey () {
      const strString = Object.fromEntries(window.location.href.replace(/.*\?/, '').split('&').map((item) => item.split('=')))
      this.key = strString.key
    },
    async getCaptcha () {
      const sid = this.$store.state.sid
      const res = await getCode(sid)
      if (res.code === 200) {
        this.svg = res.data
      }
    },
    async submit () {
      const valid = await this.$refs.observer.validate()
      if (!valid) {
        return
      }
      const pass = {
        sid: this.$store.state.sid,
        code: this.code,
        password: this.password,
        key: this.key
      }
      const res = await resetUserPass(pass)
      if (res.code === 200) {
        this.$alert(res.msg)
        this.$router.push('/login')
      } else {
        if (res.msg.msg) {
          this.$alert(res.msg.msg)
        }
        this.$refs.observer.setErrors(res.msg)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
